package com.nativetalk.common.util;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.TdNtTeacherGetui;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.bean.teacher.TdNtTeacherState;
import com.nativetalk.common.Constant;
import com.nativetalk.common.getui.AndroidTeacherGetui;
import com.nativetalk.common.getui.IosTeacherGetui;
import com.nativetalk.dao.TdNtLiveDao;
import io.netty.channel.Channel;
import oracle.jdbc.driver.Const;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/1/7.
 */
public class CallTeacher extends Thread{
    private static Logger logger = Logger.getLogger(CallTeacher.class);
    public void run(TdNtLiveDao tdNtLiveDao, int sst_id,
                    String memb_name, String memb_head_portrait, String cou_english_name, TdNtTeacherGetui tdNtTeacherGetui, Channel channel)
    {
        RetInfo retInfo = new RetInfo();
        retInfo.setMark("0");
        retInfo.setTip("预计时间");
        Map<String, Object> result = new HashMap<>();
        result.put("type","estimateTime");
        result.put("time",30);
        result.put("sst_id",sst_id);
        retInfo.setObj(result);
        channel.writeAndFlush(BeanToJson.RetInfoToJson(retInfo));
        if (MemcachedUtils.get(tdNtTeacherGetui.getToken()) != null) {
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(tdNtTeacherGetui.getToken());
            if (tdNtTeacherResult.getEqu_client_id() != null && !(tdNtTeacherResult.getEqu_client_id().equals("1212313fasdf"))) {
                if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                    AndroidTeacherGetui.apnpush("NativeTalk","One student sent you a teaching request, check now! ",
                            tdNtTeacherGetui.getEqu_client_id(),sst_id,memb_name,memb_head_portrait
                            ,tdNtTeacherGetui.getToken() + sst_id,tdNtTeacherGetui.getTea_head_portrait(),tdNtTeacherGetui.getTea_name(),cou_english_name);
                } else {
                    logger.info(tdNtTeacherGetui.getEqu_client_id());
                    IosTeacherGetui.apnpush("NativeTalk","One student sent you a teaching request, check now! ",
                            tdNtTeacherGetui.getEqu_client_id(),sst_id,memb_name,memb_head_portrait
                            ,tdNtTeacherGetui.getToken() + sst_id,tdNtTeacherGetui.getTea_head_portrait(),tdNtTeacherGetui.getTea_name(),cou_english_name);

                }

            }


        }
        logger.info("tea:"+ Constant.teacher_appId);
        int i = 0;
        while(true){
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(sst_id);
            try {
                i = i+1;
                sleep(1000);
                //logger.info(i);
                //这里可以写你自己要运行的逻辑代码
                if ( !(tdNtStudyTeachDetail.getStudy_state().equals("0")) || i >= 120 ) {
                    if (tdNtStudyTeachDetail.getStudy_number().equals("6")) {
                        channel.close();
                    }
                    break;
                }

                /**
                 *
                 * 30秒不接单把老师信息放入缓存
                 */
                if (i == 30) {
                    if (MemcachedUtils.get("order" + tdNtTeacherGetui.getTea_id()) != null){
                        int size = Integer.valueOf(MemcachedUtils.get("order" + tdNtTeacherGetui.getTea_id()).toString());
                        size = size + 1;
                        MemcachedUtils.replace("order" + tdNtTeacherGetui.getTea_id(),size,2*60*60*1000);
                        /**
                         * 将老师修改为不在线
                         */
                        TdNtTeacherState tdNtTeacherState =  new TdNtTeacherState();
                        tdNtTeacherState.setTea_id(tdNtTeacherGetui.getTea_id());
                        tdNtTeacherState.setState(Constant.Teacher.State.offline);
                        tdNtTeacherState.setState_order(Constant.Teacher.StateOrder.offline);
                        tdNtLiveDao.updateTdNtTeacherState(tdNtTeacherState);
                    } else {
                        MemcachedUtils.add("order" + tdNtTeacherGetui.getTea_id(),1,2*60*60*1000);
                    }
                    System.out.println("!23131314=" + MemcachedUtils.get("order" + tdNtTeacherGetui.getTea_id()));
                }
                if (tdNtStudyTeachDetail.getStudy_number().equals("6")) {
                    channel.close();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}
